/**
 * 
 */
package com.demo2do.fms.scheduler.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.fms.scheduler.dao.fms.FmsDao;
import com.demo2do.fms.scheduler.dao.fms.FmsDaoSupport;
import com.demo2do.fms.scheduler.entity.Vendor;
import com.demo2do.fms.scheduler.service.VendorWriter;

/**
 * @author Ysy
 *
 */
@Service("vendorWriter")
public class VendorWriterImpl implements VendorWriter {
	
	private static final Logger logger = LoggerFactory.getLogger(VendorWriterImpl.class);
    
    @Dao
    private FmsDao fmsDao;
    
    @Autowired
    private FmsDaoSupport fmsDaoSupport;
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.scheduler.service.VendorWriter#sync(com.demo2do.fms.scheduler.entity.Vendor)
     */
    public void sync(Vendor eppVendor) {
        
        String eppVendorId = eppVendor.getId();
        long eppVendorVersion = eppVendor.getVersion().longValue();
        
        logger.trace("#sync() - vendorId = [{}], vendorVersion = [{}]", eppVendorId, eppVendorVersion);
        
        // 1. get fmsVendor by eppVendorId
        Vendor fmsVendor = fmsDao.getVendor(eppVendorId);
        
        if (fmsVendor == null) {		// 2. if not exist, save
        	
        	fmsDaoSupport.saveVendor(eppVendor);
        	
        } else {					// exist
        	
        	// 3. exist and fms_vendor_version less than epp_vendor_version, edit
        	long fmsVendorVersion = fmsVendor.getVersion().longValue();
        	if (fmsVendorVersion < eppVendorVersion) {
        		fmsDao.edit(eppVendor.edit());
        	}
        	
        }
        
    }

    
}
